package com.bestillet.kyllingogco;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class InfoActivity extends Activity {
	private ImageView image;
	private TextView name, price, description;
	private Context context;
	private Button back;

	private ShopApplication app;
	private ImageTask task;
	private Menu menu;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info);
		
		context = this;

		app = (ShopApplication) getApplicationContext();
		
		menu = getIntent().getParcelableExtra("menu");
		
		image = (ImageView) findViewById(R.id.imgInfoImage);
		
		name = (TextView) findViewById(R.id.lblInfoName);
		name.setText(Html.fromHtml(menu.getName().replace("\\n", "<br>")));
		
		price = (TextView) findViewById(R.id.lblInfoPrice);
		price.setText("Pris " + menu.getPrice() + ",-");
		
		description = (TextView) findViewById(R.id.lblInfoDescription);
		description.setText(Html.fromHtml(menu.getDescription().replace("\\n", "<br>")));
		
		back = (Button) findViewById(R.id.btnInfoBack);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		if (menu.getImage() == null) {
			Bitmap b = app.getBitmapFromMemCache(menu.getPicturenormal());
			if (b != null) {
				if (b.isRecycled()) {
					task = new ImageTask();
					task.execute();
				} else {
					image.setImageBitmap(b);
				}
			} else {
				task = new ImageTask();
				task.execute();
			}
		}
	}
	
	private class ImageTask extends AsyncTask<Void, Void, Boolean> {
    	private Bitmap b;
		
		@Override
		protected Boolean doInBackground(Void... params) {
    		try {
    			InputStream in = Server.get(Server.PRODUCT_IMAGES + menu.getPicturenormal());
    			b = Server.downloadBitmap(in, (Activity)context);
    			in.close();
    			menu.setImage(b);
    			app.addBitmapToMemoryCache(menu.getPicturenormal(), b);
			} catch (IOException e) {
    			Log.e("Shop", "IOException: " + e.getMessage());
				return false;
			} catch (NullPointerException e) {
    			Log.e("Shop", "NullPointerException: " + e.getMessage());
				return false;
			} catch (OutOfMemoryError e) {
    			Log.e("Shop", "OutOfMemoryError: " + e.getMessage());
				return false;
			}
    		return true;
    	}
    
	    @Override
	    protected void onPostExecute(Boolean success) {
	    	if (success) {
    			runOnUiThread(new Runnable() {
    				@Override
    				public void run() {
    					try {
	    	    			image.setImageBitmap(b);
    					} catch (NullPointerException e) {
    						Log.e("Shop", "NullPointerException: " + e.getMessage());
    					}
    				}
    			});
	    	}
	    }
    }
}